import axios from "../../http/axios.js";
import {
  apiAddServingPatients,
  apiEditServingPatients,
  apiGetUserPatientOne
} from "../../http/server.js";
import {
  regExpName,
  regExpPhone,
  regExpIdCard
} from "../../utils/regular.js";

Page({
  data: {
    patientId: 123, // 编辑时候的患者Id
    type: "add", // 添加 | 编辑联系人
    van_pickerShow: false, // 选择器显示隐藏
    columns: ["男", "女"], // 选择器组件
    contact: {
      titleOne: {
        id: 1,
        value: "贾伟",
        placeholder: "请输入患者姓名",
        length: 5
      },
      titleTwo: {
        id: 2,
        value: "1393531102",
        placeholder: "请输入患者身份证号码",
        length: 18
      },
      titleFive: {
        id: 3,
        value: "1393531102",
        placeholder: "请输入患者手机号码"
      },
      titleThree: {
        id: 1,
        key: "请选择患者性别",
        sex: "男"
      },
      titleFour: {
        id: 2,
        key: "请选择与患者关系",
        relationship: "父母"
      }
    },
    idIndex: 1, //  初始弹出的索引值
    id: "1" // 当前选择的是哪个栏目的选择
  },
  onLoad: function(options) {
    let type = options.type;
    this.setData({
      type
    });
    if (type == "add") { //从联系人到添加联系人界面
      this.initAddData();
    } else { //type == "edit" 编辑
      let patientId = options.patientId;
      this.getPatientOne(patientId);
    }
  },
  onReady: function() {},
  onShow: function() {},
  // 添加患者情况下
  initAddData: function() {
    let contact = {
      titleOne: {
        id: 1,
        value: "",
        placeholder: "请输入患者姓名",
        length: 5
      },
      titleTwo: {
        id: 2,
        value: "",
        placeholder: "请输入患者身份证号码",
        length: 18
      },
      titleFive: {
        id: 3,
        value: "",
        placeholder: "请输入患者手机号码"
      },
      titleThree: {
        id: 1,
        key: "请选择患者性别",
        sex: "男"
      },
      titleFour: {
        id: 2,
        key: "请选择与患者关系",
        relationship: "父母"
      }
    }
    this.setData({
      contact,
      idIndex: 0
    })
  },
  // 编辑情况下获取当前编辑患者的信息
  getPatientOne: function(patientId) {
    // 等待再次来处理这些数据
    axios({
      url: apiGetUserPatientOne,
      data: {
        patientId
      }
    }).then(res => {
      let contact = {
        titleOne: {
          id: 1,
          value: res.userName,
          placeholder: "请输入患者姓名"
        },
        titleTwo: {
          id: 2,
          value: res.idCard,
          placeholder: "请输入患者身份证号码"
        },
        titleFive: {
          id: 3,
          value: res.phone,
          placeholder: "请输入患者手机号码"
        },
        titleThree: {
          id: 1,
          key: res.sex == 1 ? "男" : "女",
          sex: "男"
        },
        titleFour: {
          id: 2,
          key: res.relation,
          relationship: "父母"
        }
      }
      this.setData({
        contact,
        idIndex: 0,
        patientId
      })
    })
  },
  // 校验用户传入的参数
  check: function() {
    let contact = this.data.contact;
    let name = contact.titleOne.value;
    let idCard = contact.titleTwo.value;
    let sex = contact.titleThree.key;
    let relationship = contact.titleFour.key;
    let phone = contact.titleFive.value;
    if (!name.length) {
      wx.showToast({
        title: "请输入患者姓名",
        icon: "none"
      })
      return false;
    }
    if (!regExpName.test(name)) {
      wx.showToast({
        title: "请输入正确的姓名",
        icon: "none"
      })
      return false;
    }
    if (!idCard) {
      wx.showToast({
        title: "请输入身份证号码",
        icon: "none"
      })
      return false;
    }
    if (!regExpIdCard.test(idCard)) {
      wx.showToast({
        title: "请输入正确的身份证号码",
        icon: "none"
      })
      return false;
    }
    if (!phone.length) {
      wx.showToast({
        title: "请输入患者手机号码",
        icon: "none"
      })
      return false;
    }
    if (!regExpPhone.test(phone)) {
      wx.showToast({
        title: "请输入正确的手机号码",
        icon: "none"
      })
      return false;
    }
    if (sex == "请选择患者性别") {
      wx.showToast({
        title: "请选择与患者性别",
        icon: "none"
      })
      return false;
    }
    if (relationship == "请选择与患者关系") {
      wx.showToast({
        title: "请选择与患者关系",
        icon: "none"
      })
      return false;
    }
    return {
      name,
      idCard,
      phone,
      sex,
      relationship
    };
  },
  // 点击完成时先判断再发请求
  handlClickSuccess: function() {
    let item = this.check();
    if (!item) {
      return;
    }
    let type = this.data.type;
    if (type == "add") { //新增
      this.addPatients(item);
    } else { //编辑
      this.editPatients(item);
    }
  },
  // 编辑服务患者的接口
  editPatients: function(item) {
    let data = {
      "userName": item.name,
      "idCard": item.idCard,
      "phone": item.phone,
      "sex": item.sex == "男" ? 1 : 2, // 紧急联系人性别 1 男 2 女
      "relation": item.relationship // 与职工的关系 本人、父母、子女、亲戚、朋友
    }
    axios({
      url: `${apiEditServingPatients}/${this.data.patientId}`,
      method: "PUT",
      data: data
    }).then(res => {
      wx.showToast({
        title: "编辑成功",
        icon: "none"
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    })
  },
  // 添加服务患者的接口
  addPatients: function(item) {
    let userInfo = wx.getStorageSync("userInfo");
    let data = {
      "userId": userInfo.userId,
      "userName": item.name,
      "idCard": item.idCard,
      "phone": item.phone,
      "sex": item.sex == "男" ? 1 : 2, // 紧急联系人性别 1 男 2 女
      "relation": item.relationship // 与职工的关系 本人、父母、子女、亲戚、朋友
    }
    axios({
      url: apiAddServingPatients,
      method: "POST",
      data: data
    }).then(res => {
      wx.showToast({
        title: "添加成功",
        icon: "none"
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    })
  },
  // 点击确定时候的处理
  onConfirm: function(e) {
    let id = this.data.id;
    let value = e.detail.value;
    let contact = this.data.contact;
    if (id == 1) { //选择性别
      contact = {
        ...contact,
        titleThree: {
          id: 1,
          key: value,
          sex: "男"
        }
      }
    } else { //选择关系
      contact = {
        ...contact,
        titleFour: {
          id: 2,
          key: value,
          relationship: "父母"
        }
      }
    }
    this.setData({
      contact,
      van_pickerShow: false
    })
  },
  // 点击蒙版关闭弹出框
  onClose: function() {
    this.setData({
      van_pickerShow: false
    })
  },
  // 点击取消隐藏弹出框
  onCancel: function() {
    this.setData({
      van_pickerShow: false
    })
  },
  // 选择信息
  handleClickSelect: function(e) {
    let id = e.detail;
    this.setData({
      id: id
    })
    // id是1代表选择患者性别
    if (id == 1) {
      let keyValue = this.data.contact.titleThree.key;
      let columns = ["男", "女"];
      columns.forEach((item, index) => {
        if (item == keyValue) {
          this.setData({
            idIndex: Number(index)
          })
        }
      })
      this.setData({
        columns: columns,
        van_pickerShow: true
      });
    } else { // id是2代表选择与患者关系
      let keyValue = this.data.contact.titleFour.key;
      let columns = ["本人", "父母", "子女", "亲戚", "朋友"];
      columns.forEach((item, index) => {
        if (item == keyValue) {
          this.setData({
            idIndex: Number(index)
          })
        }
      })
      this.setData({
        columns: columns,
        van_pickerShow: true
      })
    }
  },
  // 输入框输入改变
  bindInput: function(e) {
    let contact = this.data.contact;
    let item = e.detail;
    if (item.id == 1) { // 患者姓名
      contact = {
        ...contact,
        titleOne: item
      }
    } else if (item.id == 2) { // 患者身份证号码
      contact = {
        ...contact,
        titleTwo: item
      }
    } else { // 患者手机号
      contact = {
        ...contact,
        titleFive: item
      }
    }
    this.setData({
      contact
    })
  }
})